<template>
  <div class="app-container" v-loading="loading">
    <!-- 添加或修改菜谱对话框 -->

    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="el-form"
    >
      <div class="disp-from-flxe">
        <el-form-item label="学校" prop="schoolId">
            {{ formTow.schoolName }}
        </el-form-item>
        <el-form-item label="供应商" prop="userId">
          {{ formTow.deptName }}
        </el-form-item>
      </div>

      <div class="disp-from-flxe">
        <el-form-item label="配送时间" prop="sendTime">
          
            {{ formTow.sendTime }}
        </el-form-item>
        <el-form-item label="采购金额" prop="money">
          <el-input-number
            v-model="form.money"
            :min="0"
            :precision="2"
            @change="cheNumMoney"
          ></el-input-number>
        </el-form-item>
      </div>

      <div class="disp-from-flxe">
        <el-form-item label="材料清单">
          <el-table border :data="form.params.details">
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column
              label="序号"
              align="center"
              type="index"
              width="50"
            />

            <el-table-column label="名称" align="center" width="200" prop="typeName">
            </el-table-column>
            <el-table-column label="规格" align="center" width="200" prop="specName">
            
            </el-table-column>
            <el-table-column label="单位" align="center" width="200" prop="brandName">
              
            </el-table-column>

            <el-table-column label="采购数量" align="center" width="200" prop="num">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.num"
                  :min="0"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column>

            <el-table-column label="采购价格" align="center" width="200" prop="price">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.price"
                  :min="0"
                  :precision="2"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column>
            <!-- <el-table-column label="已收数量" align="center" width="200" prop="shouNum">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.shouNum"
                  :min="0"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column>
            <el-table-column label="未收数量" align="center" width="200" prop="weiNum">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.weiNum"
                  :min="0"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column>
            <el-table-column label="收货均价" align="center" width="200" prop="jMoney">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.jMoney"
                  :min="0"
                  :precision="2"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column> -->
            <el-table-column label="收货金额" align="center" width="200" prop="money">
              <template slot-scope="scope">
                <el-input-number
                  v-model="scope.row.money"
                  :min="0"
                  :precision="2"
                  @change="(e) => cheNum(e, scope)"
                />
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" width="200" prop="remark">
              <!-- <template slot-scope="scope">
                <el-input
                  v-model="scope.row.remark"
                  :min="0"
                  @change="(e) => cheNum(e, scope)"
                />
              </template> -->
            </el-table-column>
            
          </el-table>
        </el-form-item>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="submitForm">确 定</el-button>
    </div>
  </div>
</template>
      
      <script>
import { listUser } from "@/api/system/user";
import { listDept } from "@/api/system/dept";
import {
  getDeliveryInfo,
  addDelivery,
  editDelivery,
} from "@/api/system/procure";
import { getFormaType } from "@/api/system/type";

import { getFormatbrandData } from "@/api/system/brand";
// import { getFormatCateData } from "@/api/system/cate";
import { getFormatSpecData } from "@/api/system/specifications";
export default {
  name: "addMenu",
  props: {
    mId: {
      type: String,
      default: 0,
    },
  },
  data() {
    return {
      loading: true,
      typeArr: [], //类型
      brandArr: [], //单位
      specArr: [], //规格
      schoolArr: [], //学校
      entArr: [], //供应商
      caseArrs: [
        { val: 0, lab: "小学" },
        { val: 1, lab: "初中" },
      ],
      formTow:{},
      // 表单参数
      form: {
        id: undefined,
        schoolId: undefined,
        userId: undefined,
        sendTime: undefined,
        money: 0,
        params: {
          details: [
            // {
            //   typeId: undefined, //分类id
            //   specId: undefined, //规格id
            //   brandId: undefined, //单位id
            //   price: undefined, //单价
            //   num: undefined, //数量
            //   money: 0, //总价
            //   id: undefined,
            // },
          ],
        },
      },
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        cate: [
          {
            required: true,
            message: "请选择类型",
            trigger: ["blur", "change"],
          },
        ],
        // param
      },
    };
  },
  created() {
    this.show();
    this.form = {
      id: undefined,
      schoolId: undefined,
      userId: undefined,
      sendTime: undefined,
      money: 0,
      params: {
        details: [
          // {
          //   typeId: undefined, //分类id
          //   specId: undefined, //规格id
          //   brandId: undefined, //单位id
          //   price: undefined, //单价
          //   num: undefined, //数量
          //   money: 0, //总价
          //   id: undefined,
          // },
        ],
      },
    };
    // console.log(this.mId)
    // console.log('this.mId')
    // console.log(this.mId)
  },
  methods: {
    cheNumMoney(e) {
      if (e == undefined) {
        e = 0.0;
        this.form.money = 0.0;
        // console.log(this.form.money)
        this.$forceUpdate();
        // form.money
      }
    },
    chType(e, scope) {
      console.log(e);
      console.log(scope);
    },
    cheNum(e, scope) {
        // console.log(scope.row[scope.column.property])
      if (e == undefined) {
        scope.row[scope.column.property] = 0;
      }
    },
    show() {
        getDeliveryInfo(this.mId).then((response) => {
            this.loading = false;
          var res = response.data;
          this.formTow = response.data;
          this.form = {
            id: res.id,
            schoolId: res.schoolId,
            userId: res.userId,
            sendTime: res.sendTime,
            money: res.money,
            params: {
              details: res.details,
            },
          };
        });
    },
    delParams(scope) {
      //   console.log(scope);
      this.form.params.details.splice(scope.$index, 1);
    },
    addParams(scope) {
      this.form.params.details.push({
        typeId: this.typeArr[0].id, //分类id
        specId: this.typeArr[0].specId, //规格id
        brandId: this.typeArr[0].brandId, //单位id
        price: 0, //单价
        num: 0, //采购数量
        money: 0, //总价
        remark: undefined, //备注
        jMoney: 0, //收货均价
        shouNum: 0, //已收数量
        weiNum: 0, //未收货数量
        id: undefined,
      });
      //   console.log(scope);
    },
    cancel() {
      this.$emit("cancel");
    },
    /** 提交按钮 */
    submitForm: function () {
      //   try {
      //     arr.forEach(function (value, index, array) {
      //       this.params.details.forEach((e, i) => {
      //         if (i == 1) {
      //           // break;
      //         }
      //       });
      //     });
      //   } catch (e) {
      //     if (e.message !== "Stop Iteration") throw e; // 只捕获我们的异常，其他异常继续抛出
      //   }
      this.$refs["form"].validate((valid) => {
        if (valid) {
          //   return;
          editDelivery(this.form).then((response) => {
            this.$modal.msgSuccess("修改成功");
            this.$emit("submitForm");
          });
        }
      });
    },
  },
};
</script>
      